package udpSocket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;

/**
 * 服务器端
 * @author yy
 *
 */
public class LoginServer {
public static void main(String[] args) {
	byte[] infos = new byte[1024];
	DatagramPacket dp = new DatagramPacket(infos, infos.length);
	try {
		DatagramSocket socket = new DatagramSocket(4000);
		socket.receive(dp);
		// 将接收到的数据包转化为字符串
		String info = new String(dp.getData(), 0, dp.getLength());
		System.out.println("客服端说：" + info);
		
		// 给客户端一个响应
		String reply = "你好，我在，请说!";
		byte[] replys = reply.getBytes();
		// 客户端地址
		SocketAddress sa = dp.getSocketAddress();
		// 将给客服端的响应打包成一个数据包
		DatagramPacket dp1 = new DatagramPacket(replys, 0, replys.length, sa);
		// 将数据包发送到客服端
		socket.send(dp1);
		
		
		// 释放资源
		socket.close();
	} catch (SocketException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
}
